<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            扫描点校验
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">客户</div>
                    <select id="customerCode">
                </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">规则</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择规则" id="scanRuleName" value="" disabled="true" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectRuleName()"></i>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" id="pointLi1">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" id="pointName1" style="width:45%">扫描点1</div>
                    <div class='aui-list-item-input'>
                        <input type="text" id="1" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" id="pointLi2">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" id="pointName2" style="width:45%">扫描点2</div>
                    <div class='aui-list-item-input'>
                        <input type="text" id="2" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" id="pointLi3">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" id="pointName3" style="width:45%">扫描点3</div>
                    <div class='aui-list-item-input'>
                        <input type="text" id="3" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" id="pointLi4">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" id="pointName4" style="width:45%">扫描点4</div>
                    <div class='aui-list-item-input'>
                        <input type="text" id="4" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" id="pointLi5">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" id="pointName5" style="width:45%">扫描点5</div>
                    <div class='aui-list-item-input'>
                        <input type="text" id="5" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" id="pointLi6">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" id="pointName6" style="width:45%">扫描点6</div>
                    <div class='aui-list-item-input'>
                        <input type="text" id="6" value="" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>

            <li class='aui-list-item' style="display:none" id="messageLi">
                <div class='aui-list-item-inner'>
                    <div class='aui-list-item-label'>消息</div>
                    <div class='aui-list-item-input'>
                        <textarea id='message'></textarea>
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2px;"></div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在
    var idSelector = "";
    //扫描点length
    var count = 0;

    //初始化查询仓库下拉框
    apiready = function() {

        //查询客户
        selectCustomer();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();

        console.log("id:" + idSelector);
    }

    //查询客户
    function selectCustomer() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "md_customer",
                column_name: "name",
                column_value: "code",
                condition: "is_delete=0",
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                var list = eval(myobj.list);
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("customerCode"), html);
            }
        });
    }

    //选择规则
    function goSelectRuleName() {
        api.openWin({
            name: 'uwmSelectScanRule',
            url: 'uwmSelectScanRule.html',
            pageParam: {
                name: "uwmScanRuleChk",
            }
        });
    }

    //扫描规则选择后回调函数
    function setRuleName(scanRuleName) {
        $("#scanRuleName").val(scanRuleName);
        onChange();
    }

    //规则下拉框值改变，查询扫描点
    function onChange() {
        //扫描规则名称
        var scanRuleName = $("#scanRuleName").val();

        //规则名称不为空，查询扫描点
        if (scanRuleName != null && scanRuleName != "" && scanRuleName != undefined) {
            var un = localStorage.getItem('un');
            var serverUrl = localStorage.getItem('serverUrl');
            var url = serverUrl + "/app/wmSearchScanPoint";
            var data = {
                values: {
                    scanRuleName: scanRuleName,
                    un: un,
                }
            };
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 300,
                returnAll: false,
                data: data
            }, function(ret, err) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    var list = eval(myobj.list);

                    //结果集大小
                    count = list.length;
                    var html = "";

                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        $("#pointLi" + (i + 1)).show();
                        $("#pointName" + (i + 1)).html(list[i].pointName);
                    }

                    var j = list.length + 1;
                    for (var i = j; i < 7; i++) {
                        $("#pointLi" + i).hide();
                    }
                    $("#message").val("");

                    if (list.length > 0) {
                        $("#messageLi").show();
                        idSelector = "1";
                        $("#" + idSelector).select();
                    } else {
                        $("#messageLi").hide();
                    }
                }
            });
        } else {
            for (var i = 1; i < 7; i++) {
                $("#pointLi" + i).hide();
            }
            $("#messageLi").hide();
        }
    }

    //扫描
    function doScan() {

        //客户
        var customerCode = $("#customerCode").val();
        if (customerCode == null || customerCode == "" || customerCode == undefined) {
            api.toast({
                msg: "请线选择客户",
                duration: 3000
            });
            return;
        }

        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {
            $("#" + idSelector).val(ret);

            if (ret != null && ret != "" && ret != undefined) {

                if (idSelector == "1") {

                    //校验扫描值是否正确
                    pointNameChange();

                } else {

                    var seqno = parseInt(idSelector);
                    var idValue = "";
                    for (var i = 1; i < seqno; i++) {
                        idValue = $("#" + i).val();
                        if (idValue == null || idValue == "" || idValue == undefined) {
                            api.toast({
                                msg: "请按顺序扫描",
                                duration: 3000
                            });
                            $("#" + idSelector).val("")
                            $("#message").val("请按顺序扫描");
                            return;
                        }
                    }

                    //校验扫描值是否正确
                    pointNameChange();
                }
            }
        });
    }

    //扫描点值是否正确
    function pointNameChange() {
        //客户
        var customerCode = $("#customerCode").val();
        //扫描规则
        var scanRuleName = $("#scanRuleName").val();
        //扫描ID
        var seqno = parseInt(idSelector);
        //扫描值
        var scanValue = $("#" + idSelector).val();

        //客户非空校验
        if (customerCode == "" || customerCode == null || customerCode == undefined) {
            api.toast({
                msg: '客户不能为空',
                duration: 3000
            });
            return;
        }

        //扫描规则非空校验
        if (scanRuleName == "" || scanRuleName == null || scanRuleName == undefined) {
            api.toast({
                msg: '扫描规则不能为空',
                duration: 3000
            });
            return;
        }

        //扫描值非空校验
        if (scanValue == "" || scanValue == null || scanValue == undefined) {
            api.toast({
                msg: '扫描值不能为空',
                duration: 3000
            });
            return;
        }

        scanValue = encodeURIComponent(scanValue)
        // console.log("scanValue:" + scanValue);

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmScanPointSave";
        var data = {
            values: {
                customerCode: customerCode, //客户
                scanRuleName: scanRuleName, //扫描规则名称
                seqno: seqno, // 扫描点顺序号
                scanValue: scanValue, //扫描值
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#message").val(myobj.message);

                    //最后一个扫描点
                    if (count == seqno) {

                        //清空所有值
                        for (var i = 1; i <= seqno; i++) {
                            $("#" + i).val("");
                        }

                        idSelector = "1";

                        //不是最后一点，则光标自动换到下一个
                    } else {

                        idSelector = "" + (seqno + 1);
                    }

                    $("#" + idSelector).select();

                    //扫描值错误
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#message").val(myobj.message);

                    //清空所有，第一点重新扫描
                    for (var i = 1; i <= seqno; i++) {
                        $("#" + i).val("");
                    }

                    idSelector = "1";
                    $("#" + idSelector).select();

                    //exception异常 是2
                } else if (myobj.code == '2') {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#message").val(myobj.message);
                }
            } else {
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    //第一个扫描点
    $("#1").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        
        if (keycode == '13') {

            //扫描点1
            $("#1").val(sm);

            //校验
            pointNameChange();
        }
    });

    //扫描点2
    $("#2").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#2").val(sm);

            var seqno = parseInt(idSelector);
            var idValue = "";
            for (var i = 1; i < seqno; i++) {
                idValue = $("#" + i).val();
                if (idValue == null || idValue == "" || idValue == undefined) {
                    api.toast({
                        msg: "请按顺序扫描",
                        duration: 3000
                    });
                    $("#" + idSelector).val("")
                    $("#message").val("请按顺序扫描");
                    return;
                }
            }

            //校验
            pointNameChange();
        }
    });

    //扫描点3
    $("#3").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#3").val(sm);

            var seqno = parseInt(idSelector);
            var idValue = "";
            for (var i = 1; i < seqno; i++) {
                idValue = $("#" + i).val();
                if (idValue == null || idValue == "" || idValue == undefined) {
                    api.toast({
                        msg: "请按顺序扫描",
                        duration: 3000
                    });
                    $("#" + idSelector).val("")
                    $("#message").val("请按顺序扫描");
                    return;
                }
            }

            //校验
            pointNameChange();
        }
    });

    //扫描点4
    $("#4").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#4").val(sm);
            var seqno = parseInt(idSelector);
            var idValue = "";
            for (var i = 1; i < seqno; i++) {
                idValue = $("#" + i).val();
                if (idValue == null || idValue == "" || idValue == undefined) {
                    api.toast({
                        msg: "请按顺序扫描",
                        duration: 3000
                    });
                    $("#" + idSelector).val("")
                    $("#message").val("请按顺序扫描");
                    return;
                }
            }

            //校验
            pointNameChange();
        }
    });

    //扫描点5
    $("#5").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#5").val(sm);
            var seqno = parseInt(idSelector);
            var idValue = "";
            for (var i = 1; i < seqno; i++) {
                idValue = $("#" + i).val();
                if (idValue == null || idValue == "" || idValue == undefined) {
                    api.toast({
                        msg: "请按顺序扫描",
                        duration: 3000
                    });
                    $("#" + idSelector).val("")
                    $("#message").val("请按顺序扫描");
                    return;
                }
            }

            //校验
            pointNameChange();
        }
    });

    //扫描点6
    $("#6").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#6").val(sm);
            var seqno = parseInt(idSelector);
            var idValue = "";
            for (var i = 1; i < seqno; i++) {
                idValue = $("#" + i).val();
                if (idValue == null || idValue == "" || idValue == undefined) {
                    api.toast({
                        msg: "请按顺序扫描",
                        duration: 3000
                    });
                    $("#" + idSelector).val("")
                    $("#message").val("请按顺序扫描");
                    return;
                }
            }

            //校验
            pointNameChange();
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
